package com.aaa.controller;


import com.aaa.aop.SaveOrUpdateEntityAnn;
import com.aaa.entity.LayUiTable;
import com.aaa.entity.News;
import com.aaa.entity.Result;
import com.aaa.service.NewsService;
import com.aaa.util.MyConstants;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController extends BaseController {
    @Autowired
    private NewsService newsService;

    @RequestMapping("/toShowNews")
    public String toShowNews(){
        return "news/showNews";
    }
    @RequestMapping("/toShowMain")
    public String toShowMain(){
        return "main";
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public LayUiTable selectAll(){
        LayUiTable table = new LayUiTable();
        List<News> newsList = newsService.selectList(null);
        table.setData(newsList);
        return table;

    }

    @RequestMapping("/selectAllNews")
    @ResponseBody
    public LayUiTable selectAllNews(Integer page, Integer limit, String searchTitle,String searchCreateBy) {
        //返回所有条数
        Wrapper<News> wrapper = new EntityWrapper<>();
        //添加模糊查询的条件
        if (null != searchTitle && !"".equals(searchTitle)) {
            wrapper.like("title", searchTitle);
        }
        if (null != searchCreateBy && !"".equals(searchCreateBy)) {
            wrapper.like("create_by",searchCreateBy);
        }
        //排序
        wrapper.orderBy("id",true);
        int newsListCount = newsService.selectCount(wrapper);
        Page<News> pageInfo = new Page(page, limit);
        Page<News> newsPage = newsService.selectPage(pageInfo, wrapper);
        //从分页结果中提取list集合
        List<News> newsList = newsPage.getRecords();
        LayUiTable table = new LayUiTable();
        table.setCode(MyConstants.OPERATION_SUCCESS_CODE);
        table.setMsg(MyConstants.OPERATION_SUCCESS_MESSAGE);
        table.setData(newsList);
        table.setCount(newsListCount);
        return table;
    }

    /**
     * 保存部门
     * @param news
     * @return
     */
    @SaveOrUpdateEntityAnn(entityClass = News.class)
    @RequestMapping("/saveNews")
    @ResponseBody
    public Result saveNews(News news) {
        boolean insert = newsService.insert(news);
        if (insert) {
            return super.success();
        } else {
            return super.error();
        }
    }

    /**
     * 更新部门信息（controller中的方法作用1：页面跳转，2：收受参数，返回参数
     * 不建议在controller中加入过多的业务逻辑）
     * @param news
     * @return
     */
    @RequestMapping("/updateNews")
    @ResponseBody
    @SaveOrUpdateEntityAnn(entityClass = News.class)
    public Result updateNews(News news) {
        //此处待优化
        boolean update = newsService.updateById(news);
        if (update) {
            return super.success();
        } else {
            return super.error();
        }

    }

    @RequestMapping("/deleteNews")
    @ResponseBody
    public Result deleteNews(News news) {
        boolean update = newsService.deleteById(news);
        if (update) {
            return super.success();
        } else {
            return super.error();
        }
    }


}
